package L01;

public class T13 {
    public static Object locker1=new Object ();
    public static Object locker2=new Object ();

    public static void main (String[] args) {
        Thread t1=new Thread (()->{
            synchronized (locker1){
                //这里的sleep()确保已经上锁后在进行后续操作
                //防止线程2还没有开始加锁，线程1就把两把锁加完了
                try {
                    Thread.sleep (1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException (e);
                }
                synchronized (locker2){
                    System.out.println ("t1已加锁");
                }
            }
        });

        Thread t2=new Thread (()->{
            synchronized (locker2){
                try {
                    Thread.sleep (1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException (e);
                }

                synchronized (locker1){
                    System.out.println ("t2已加锁");
                }
            }
        });

        t1.start ();
        t2.start ();
    }

}
